package com.ruoyi.business.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import com.ruoyi.business.domain.Shop;
import com.ruoyi.business.domain.vo.TreeSelect;
import com.ruoyi.common.core.utils.SpringUtils;

import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.ServiceTypeMapper;
import com.ruoyi.business.domain.ServiceType;
import com.ruoyi.business.service.IServiceTypeService;

/**
 * 服务类别管理Service业务层处理
 * 
 * @author gyz
 * @date 2024-09-11
 */
@Service
public class ServiceTypeServiceImpl implements IServiceTypeService 
{
    @Autowired
    private ServiceTypeMapper serviceTypeMapper;


    /**
     * 查询服务类别管理
     * 
     * @param id 服务类别管理主键
     * @return 服务类别管理
     */
    @Override
    public ServiceType selectServiceTypeById(Long id)
    {
        return serviceTypeMapper.selectServiceTypeById(id);
    }

    /**
     * 查询服务类别管理列表
     * 
     * @param serviceType 服务类别管理
     * @return 服务类别管理
     */
    @Override
    public List<ServiceType> selectServiceTypeList(ServiceType serviceType)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Set<String> roles = loginUser.getRoles();

        if (roles.contains("business")&&!SecurityUtils.isAdmin(loginUser.getUserid())) {
            String username = loginUser.getUsername();
            serviceType.setUserName(username);
            return serviceTypeMapper.selectServiceTypeListByUserName(serviceType);
        }
        return serviceTypeMapper.selectServiceTypeList(serviceType);
    }

    /**
     * 新增服务类别管理
     * 
     * @param serviceType 服务类别管理
     * @return 结果
     */
    @Override
    public int insertServiceType(ServiceType serviceType)
    {
        return serviceTypeMapper.insertServiceType(serviceType);
    }

    /**
     * 修改服务类别管理
     * 
     * @param serviceType 服务类别管理
     * @return 结果
     */
    @Override
    public int updateServiceType(ServiceType serviceType)
    {
        return serviceTypeMapper.updateServiceType(serviceType);
    }

    /**
     * 批量删除服务类别管理
     * 
     * @param ids 需要删除的服务类别管理主键
     * @return 结果
     */
    @Override
    public int deleteServiceTypeByIds(Long[] ids)
    {
        return serviceTypeMapper.deleteServiceTypeByIds(ids);
    }

    /**
     * 删除服务类别管理信息
     * 
     * @param id 服务类别管理主键
     * @return 结果
     */
    @Override
    public int deleteServiceTypeById(Long id)
    {
        return serviceTypeMapper.deleteServiceTypeById(id);
    }

    @Override
    public List<TreeSelect> selectTypeTreeList(ServiceType serviceType) {
        List<ServiceType> serviceTypes = serviceTypeMapper.selectServiceTypeList(serviceType);
        return buildDeptTreeSelect(serviceTypes);
    }
    /**
     * 构建前端所需要树结构
     *
     * @param serviceTypes 类别列表
     * @return 树结构列表
     */
    @Override
    public List<ServiceType> buildTypeTree(List<ServiceType> serviceTypes)
    {
        List<ServiceType> returnList = new ArrayList<ServiceType>();
        for (ServiceType serviceType: serviceTypes) {
            List<Shop> shops = serviceTypeMapper.selectTypeShopByTypeId(serviceType.getId());
            /*List<Shop> returnShopList=new ArrayList<>();
            for (Shop shop: shops) {
                TreeSelect treeSelect = new TreeSelect(shop);
                System.out.println(shop);
                returnShopList.add(shop);
            }*/
            shops.stream().map(TreeSelect::new).collect(Collectors.toList());

            serviceType.setChildren(shops);
        }
        if (returnList.isEmpty())
        {
            returnList = serviceTypes;
        }
        return returnList;
    }

    /**
     * 构建前端所需要下拉树结构
     *
     * @param serviceTypes 类别列表
     * @return 下拉树结构列表
     */
    @Override
    public List<TreeSelect> buildDeptTreeSelect(List<ServiceType> serviceTypes)
    {
        List<ServiceType> serviceTypes1Trees = buildTypeTree(serviceTypes);
        return serviceTypes1Trees.stream().map(TreeSelect::new).collect(Collectors.toList());
    }
}
